package com.winit.openapi.gt.controller;

import java.util.*;

import javax.annotation.Resource;

import com.alibaba.fastjson.JSONArray;
import com.winit.oms.spi.taskcommand.command.*;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSONObject;
import com.winit.common.query.Page;
import com.winit.common.spi.context.CommandContext;
import com.winit.erp.spi.bpartner.vo.Bpartner;
import com.winit.oms.spi.common.vo.PageVo;
import com.winit.oms.spi.taskcommand.DeductionOrderService;
import com.winit.oms.spi.taskcommand.vo.DeductionOrderDeliveryStandardVo;
import com.winit.oms.spi.taskcommand.vo.DeductionOrderDetailVo;
import com.winit.oms.spi.taskcommand.vo.DeductionOrderVo;
import com.winit.openapi.constants.ApiConstant;
import com.winit.openapi.constants.ErrorCode;
import com.winit.openapi.controller.BaseController;
import com.winit.openapi.exception.ApiException;
import com.winit.openapi.model.RequestMsg;
import com.winit.openapi.model.ResponseMsg;
import com.winit.openapi.util.DateUtil;
import com.winit.openapi.util.ValidateUtil;

/**
 * 扣货单
 * 
 * @version <pre>
 * Author	Version		Date		Changes
 * qiyongkang 	1.0  		2017年11月29日 	Created
 * </pre>
 * @since 1.
 */
@Controller
@RequestMapping(value = "/deductionOrder")
public class DeductionOrderController extends BaseController {

    @Resource
    private DeductionOrderService deductionOrderService;

    @RequestMapping(value = "/queryPage")
    public String queryPage(@ModelAttribute RequestMsg requestMsg, @ModelAttribute ResponseMsg responseMsg) {
        // 获取参数
        JSONObject json = null;
        if (requestMsg.getData() instanceof JSONObject) {
            json = JSONObject.parseObject(requestMsg.getData().toString());
        } else {
            throw new ApiException(ErrorCode.FIELD_TYPE_ERROR, "data");
        }

        Map<String, Integer> pageParams = this.buildPageParams(json);
        PageVo pageVo = new PageVo();
        pageVo.setPageNo(pageParams.get("pageNo"));
        pageVo.setPageSize(pageParams.get("pageSize"));

        // 查询
        SearchInboundDeductionOrderCommand command = new SearchInboundDeductionOrderCommand();
        command.setPageVo(pageVo);
        // 客户编码
        Bpartner bpartner = CommandContext.getContext().get(ApiConstant.API_BPARTNER);
        if (bpartner != null) {
            command.setCustomerCode(bpartner.getValue());
        } else {
            command.setCustomerCode(CommandContext.getContext().getUserId());
        }
        command.setWinitOrderNo(json.getString("winitOrderNo"));
        command.setInspectionWhCode(json.getString("inspectionWhCode"));
        command.setDestinationWhCode(json.getString("destinationWhCode"));
        if (json.containsKey("createdFrom")) {
            String orderDateStart = json.getString("createdFrom");
            orderDateStart += " 00:00:00";
            Date from = DateUtil.parse2Date(orderDateStart, DateUtil.FULL_DATE_STRING);
            command.setCreatedFrom(from);
        }
        if (json.containsKey("createdTo")) {
            String orderDateEnd = json.getString("createdTo");
            orderDateEnd += " 23:59:59";
            Date to = DateUtil.parse2Date(orderDateEnd, DateUtil.FULL_DATE_STRING);
            command.setCreatedTo(to);
        }
        command.setStatus(json.getString("status"));
        command.setCtx(CommandContext.getContext());
        command.setWinitProductionLine(json.getString("winitProductionLine"));
        command.setDeductionReson(json.getString("deductionReson"));// 只查询不符合FBA扣货标准的扣货单
        Page<DeductionOrderVo> page = deductionOrderService.queryDeductionOrderPage(command);

        if (page != null) {
            Map<String, Object> pageMap = new HashMap<String, Object>();
            pageMap.put("pageNo", page.getPageable().getPageNumber());
            pageMap.put("pageSize", page.getPageable().getPageSize());
            pageMap.put("totalCount", page.getTotalElements());

            Map<String, Object> data = new HashMap<String, Object>();
            data.put("list", page.getContent());
            data.put("pageParams", pageMap);
            responseMsg.setData(data);
        }
        return SUCCESS;
    }

    @RequestMapping(value = "/getDetail")
    public String getDetail(@ModelAttribute RequestMsg requestMsg, @ModelAttribute ResponseMsg responseMsg) {
        // 获取参数
        JSONObject json = null;
        if (requestMsg.getData() instanceof JSONObject) {
            json = JSONObject.parseObject(requestMsg.getData().toString());
        } else {
            throw new ApiException(ErrorCode.FIELD_TYPE_ERROR, "data");
        }

        // 校验
        ValidateUtil.isNotNull(json, "deductionNo");

        // 查询
        SearchInboundDeductionOrderCommand command = new SearchInboundDeductionOrderCommand();
        command.setDeductionNo(json.getString("deductionNo"));
        command.setCtx(CommandContext.getContext());
        command.setWinitProductionLine(json.getString("winitProductionLine"));
        DeductionOrderVo deductionOrderVo = deductionOrderService.getDeductionOrderVoByDeductionNo(command);
        responseMsg.setData(deductionOrderVo);
        return SUCCESS;
    }

    @RequestMapping(value = "/queryDeliveryStandardPage")
    public String queryDeliveryStandardPage(@ModelAttribute RequestMsg requestMsg,
                                            @ModelAttribute ResponseMsg responseMsg) {
        // 获取参数
        JSONObject json = null;
        if (requestMsg.getData() instanceof JSONObject) {
            json = JSONObject.parseObject(requestMsg.getData().toString());
        } else {
            throw new ApiException(ErrorCode.FIELD_TYPE_ERROR, "data");
        }

        // 校验
        ValidateUtil.isNotNull(json, "winitOrderNo");

        Map<String, Integer> pageParams = this.buildPageParams(json);
        PageVo pageVo = new PageVo();
        pageVo.setPageNo(pageParams.get("pageNo"));
        pageVo.setPageSize(pageParams.get("pageSize"));

        // 查询
        DeductionOrderDeliveryStandardQueryCommand command = new DeductionOrderDeliveryStandardQueryCommand();
        command.setPageVo(pageVo);
        DeductionOrderDeliveryStandardVo vo = new DeductionOrderDeliveryStandardVo();
        vo.setWinitOrderNo(json.getString("winitOrderNo"));
        command.setVo(vo);
        command.setCtx(CommandContext.getContext());
        Page<DeductionOrderDeliveryStandardVo> page = deductionOrderService.findDeductionOrderDeliveryStandardPage(command);
        if (page != null) {

            Map<String, Object> pageMap = new HashMap<String, Object>();
            pageMap.put("pageNo", page.getPageable().getPageNumber());
            pageMap.put("pageSize", page.getPageable().getPageSize());
            pageMap.put("totalCount", page.getTotalElements());

            Map<String, Object> data = new HashMap<String, Object>();
            data.put("list", page.getContent());
            data.put("pageParams", pageMap);
            responseMsg.setData(data);
        }
        return SUCCESS;
    }

    @RequestMapping(value = "/queryDetailPage")
    public String queryDeductionOrderDetailPage(@ModelAttribute RequestMsg requestMsg,
                                                @ModelAttribute ResponseMsg responseMsg) {
        // 获取参数
        JSONObject json = null;
        if (requestMsg.getData() instanceof JSONObject) {
            json = JSONObject.parseObject(requestMsg.getData().toString());
        } else {
            throw new ApiException(ErrorCode.FIELD_TYPE_ERROR, "data");
        }

        // 校验
        ValidateUtil.isNotNull(json, "deductionNo");
        Map<String, Integer> pageParams = this.buildPageParams(json);
        PageVo pageVo = new PageVo();
        pageVo.setPageNo(pageParams.get("pageNo"));
        pageVo.setPageSize(pageParams.get("pageSize"));

        // 查询
        SearchInboundDeductionOrderDetailCommand command = new SearchInboundDeductionOrderDetailCommand();
        command.setPageVo(pageVo);
        command.setDeductionNo(json.getString("deductionNo"));
        command.setWinitOrderNo(json.getString("winitOrderNo"));
        command.setCtx(CommandContext.getContext());
        command.setWinitProductionLine(json.getString("winitProductionLine"));
        Page<DeductionOrderDetailVo> page = deductionOrderService.queryDeductionOrderDetailPage(command);
        if (page != null) {
            Map<String, Object> pageMap = new HashMap<String, Object>();
            pageMap.put("pageNo", page.getPageable().getPageNumber());
            pageMap.put("pageSize", page.getPageable().getPageSize());
            pageMap.put("totalCount", page.getTotalElements());

            Map<String, Object> data = new HashMap<String, Object>();
            data.put("list", page.getContent());
            data.put("pageParams", pageMap);
            responseMsg.setData(data);
        }
        return SUCCESS;
    }

    @RequestMapping(value = "/queryCheckMerchandiseList")
    public String queryCheckMerchandiseList(@ModelAttribute RequestMsg requestMsg,
                                            @ModelAttribute ResponseMsg responseMsg) {
        // 获取参数
        JSONObject json = null;
        if (requestMsg.getData() instanceof JSONObject) {
            json = JSONObject.parseObject(requestMsg.getData().toString());
        } else {
            throw new ApiException(ErrorCode.FIELD_TYPE_ERROR, "data");
        }

        // 校验
        ValidateUtil.isNotNull(json, "deductionNo");
        ValidateUtil.isNotNull(json, "packageSerno");
        // 查询
        SearchInboundDeductionOrderDetailCommand command = new SearchInboundDeductionOrderDetailCommand();
        command.setDeductionNo(json.getString("deductionNo"));
        command.setPackageSerno(json.getString("packageSerno"));
        command.setWinitProductionLine(json.getString("winitProductionLine"));
        command.setCtx(CommandContext.getContext());
        List<DeductionOrderDetailVo> detailList = deductionOrderService.queryCheckMerchandiseList(command);
        if (detailList != null) {
            Map<String, Object> data = new HashMap<String, Object>();
            data.put("list", detailList);
            responseMsg.setData(data);
        }
        return SUCCESS;
    }

    @RequestMapping(value = "/queryDSBDetailPage")
    public String queryDeductionOrderDSBDetailPage(@ModelAttribute RequestMsg requestMsg,
                                                   @ModelAttribute ResponseMsg responseMsg) {
        // 获取参数
        JSONObject json = null;
        if (requestMsg.getData() instanceof JSONObject) {
            json = JSONObject.parseObject(requestMsg.getData().toString());
        } else {
            throw new ApiException(ErrorCode.FIELD_TYPE_ERROR, "data");
        }

        // 校验
        ValidateUtil.isNotNull(json, "deductionNo");

        Map<String, Integer> pageParams = this.buildPageParams(json);
        PageVo pageVo = new PageVo();
        pageVo.setPageNo(pageParams.get("pageNo"));
        pageVo.setPageSize(pageParams.get("pageSize"));

        // 查询
        SearchDeductionOrderCommand command = new SearchDeductionOrderCommand();
        command.setPageVo(pageVo);
        command.setWinitOrderNo(json.getString("winitOrderNo"));
        command.setWinitProductionLine(json.getString("winitProductionLine"));
        command.setDeductionNo(json.getString("deductionNo"));
        command.setCtx(CommandContext.getContext());
        Page<DeductionOrderDetailVo> page = deductionOrderService.queryDeductionOrderDSBDetailPage(command);
        if (page != null) {
            Map<String, Object> pageMap = new HashMap<String, Object>();
            pageMap.put("pageNo", page.getPageable().getPageNumber());
            pageMap.put("pageSize", page.getPageable().getPageSize());
            pageMap.put("totalCount", page.getTotalElements());

            Map<String, Object> data = new HashMap<String, Object>();
            data.put("list", page.getContent());
            data.put("pageParams", pageMap);
            responseMsg.setData(data);
        }
        return SUCCESS;
    }

    @RequestMapping(value = "/confirmDeduction")
    public String confirmDeduction(@ModelAttribute RequestMsg requestMsg, @ModelAttribute ResponseMsg responseMsg) {
        // 获取参数
        JSONObject json = null;
        if (requestMsg.getData() instanceof JSONObject) {
            json = JSONObject.parseObject(requestMsg.getData().toString());
        } else {
            throw new ApiException(ErrorCode.FIELD_TYPE_ERROR, "data");
        }

        DeductionOrderCommand command = new DeductionOrderCommand();
        command.setWinitProductionLine(json.getString("winitProductionLine"));
        command.setWinitOrderNo(json.getString("winitOrderNo"));
        command.setDeductionNo(json.getString("deductionNo"));
        command.setDeductionType(json.getString("deductionType"));
        command.setDeductionReson(json.getString("deductionReson"));
        command.setStatus(json.getString("status"));
        JSONArray array = json.getJSONArray("listDeductionOrderDetails");
        if (null != array && array.size() > 0) {
            // 需要扣货的包裹
            List<DeductionOrderDetailCommand> detailList = new ArrayList<DeductionOrderDetailCommand>();
            for (Object obj : array) {
                DeductionOrderDetailCommand detail = new DeductionOrderDetailCommand();
                JSONObject detailObj = (JSONObject)obj;
                detail.setId(detailObj.getLong("id"));
                detail.setPackageSerno(detailObj.getString("packageSerno"));
                detailList.add(detail);
            }
            command.setListDeductionOrderDetails(detailList);
        }
        deductionOrderService.confirmDeduction(command);
        return SUCCESS;
    }

    @RequestMapping(value = "/cancelDeductionOrder")
    public String cancelDeductionOrder(@ModelAttribute RequestMsg requestMsg, @ModelAttribute ResponseMsg responseMsg) {
        // 获取参数
        JSONObject json = null;
        if (requestMsg.getData() instanceof JSONObject) {
            json = JSONObject.parseObject(requestMsg.getData().toString());
        } else {
            throw new ApiException(ErrorCode.FIELD_TYPE_ERROR, "data");
        }
        // 校验扣货单号
        ValidateUtil.isNotNull(json, "deductionNo");
        // 校验产品线
        ValidateUtil.isNotNull(json, "winitProductionLine");
        DeductionOrderCommand command = new DeductionOrderCommand();
        command.setWinitProductionLine(json.getString("winitProductionLine"));
        command.setDeductionNo(json.getString("deductionNo"));
        command.setCtx(CommandContext.getContext());
        deductionOrderService.cancelDeductionOrder(command);
        return SUCCESS;
    }
}
